package com.mk.cloud.pt.controller.app;

import act.apidoc.Description;
import act.controller.Controller;
import act.handler.NonBlock;
import act.inject.DefaultValue;
import act.util.CacheFor;
import com.mk.cloud.common.domain.PArticle;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.interceptor.header.HeaderService;
import com.mk.cloud.common.interceptor.header.IHLogin;
import com.mk.cloud.common.util.ResUtils;
import com.mk.cloud.pt.service.app.PCompanyService;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.http.H;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("app/article")
public class PArticleController extends HeaderService {

    @Inject
    private PArticle.Mapper pArticleMapper;
    @Inject
    private PCompanyService pCompanyService;

    @Description("文章列表")
    @GetAction("pageList.json")
//    @With(IHLogin.class)
    @CacheFor(value = 10 * 60)
    @NonBlock
    public String pArticleList(
            @Description("小程序appid") String appid,
            @Description("筛选条件-文章类型") String arttype,
            @Description("当前第几页") @DefaultValue("1") Integer page,
            @Description("每页多少条") @DefaultValue("15") Integer size

    ) {
        PageQuery<PArticle> query = new PageQuery<>();
        query.setPageSize(size);
        query.setPageNumber(page);
        Map<String, Object> map = new HashMap<>();
        map.put("arttype", arttype);
        map.put("coid", pCompanyService.getCompanyByAppid(appid).getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pArticleMapper.getSQLManager().pageQuery("pArticle.select_article_page_list", PArticle.class, query);
        return ResUtils.OK(query.getList());
    }


    @Description("文章--文章详情")
    @GetAction("get.json")
    @CacheFor(value = 10 * 60)
    @NonBlock
    public String getPArticle(@Description("文章id") String artid) {
        if (S.isEmpty(artid)) {
            return ResUtils.OK(new PArticle());
        }
        return ResUtils.OK(pArticleMapper.single(artid));
    }

    @Description("文章--文章详情,根据文章类型获取")
    @GetAction("getrewarddetail.json")
//    @With(IHLogin.class)
    @CacheFor(value = 10 * 60)
    @NonBlock
    public String getPArticleByArttype(
            @Description("小程序appid") @DefaultValue("aaa") String appid,
            @Description("文章类型") @DefaultValue("1") String arttype
    ) {
        PageQuery<PArticle> query = new PageQuery<>();
        query.setPageSize(1);
        query.setPageNumber(1);
        Map<String, Object> map = new HashMap<>();
        map.put("arttype", arttype);
        map.put("coid", pCompanyService.getCompanyByAppid(appid).getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pArticleMapper.getSQLManager().pageQuery("pArticle.select_article_page_list", PArticle.class, query);
        List<PArticle> list = query.getList();
        if(list!=null&&list.size()>0){
            return ResUtils.OK(pArticleMapper.single(list.get(0).getArtid()));
        }
        return ResUtils.OK(new PArticle());
    }



}
